<?php
/**
 * 系统主页
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 16:34
 */
namespace app\publics\controller;

use think\Db;
use thinkcms\auth\Auth;
use thinkcms\auth\library\Tree;
use thinkcms\auth\model\Menu;
use app\common\model\UserModel;
use app\common\model\ConfigModel;
use app\common\model\OrderModel;


class Index extends Base
{
    public function index()
    {
        return $this->fetch();
    }

    // 主页
    public function home()
    {   
        $user = new UserModel();
        $conf = new ConfigModel();
        $order = new OrderModel();

        //总数
        $user_totl = $user->count();
        //普通用户
        $user_person = $user->where('type',0)->count();
        //内部账号
        $user_spe = $user_totl - $user_person;

        //用户钻石总数
        $balance_totl = $user->where('type',0)->sum('balance');
        //充值钻石
        //ORDER_TYPE_WITHDRAWAL
        //ORDER_TYPE_RECHARGE

        $balance_re = $order->where(['deal_type'=>ORDER_TYPE_RECHARGE,'status'=>1])->sum('deal_amount');
        $balance_WR = $order->where(['deal_type'=>ORDER_TYPE_WITHDRAWAL,'status'=>1])->sum('deal_amount');

        // 获取今天
        $today = $user->where('type',0)->whereTime('create_time', 'today')->count();
        // 获取昨天
        $yesterday = $user->where('type',0)->whereTime('create_time', 'yesterday')->count(); 
        //上周
        $lastweek = $user->where('type',0)->whereTime('create_time','last week')->count();
        // 获取本周
        $week = $user->where('type',0)->whereTime('create_time', 'week')->count(); 
        // 获取本月
        $month = $user->where('type',0)->whereTime('create_time', 'month')->count(); 


        $limit = empty(input('param.limit')) ? 6 : input('param.limit');
        $query = [
            'limit' => $limit
        ];
        $list  = Db::name('action_log')
            ->order('create_time desc')
            ->paginate($limit, [
                'query' => $query
            ]);

        $this->assign([
            'user_totl'=>$user_totl,
            'user_person'=> $user_person,
            'user_spe'=>$user_spe,
            'today'=>$today,
            'yesterday'=>$yesterday,
            'week'=>$week,
            'lastweek'=>$lastweek,
            'month'=>$month,
            'balance_totl'=>$balance_totl,
            'balance_re'=>$balance_re,
            'balance_WR'=>$balance_WR,
            'list' => $list
            ]);
        return $this->fetch();
    }

    // 菜单管理
    public function menu()
    {
        $menu = Auth::menuCheck();
        $tree = new Tree();

        // 菜单结构
        $new_menu = [];
        $count = 0;
        foreach ($menu as $k => &$v) {
            $url = $v['url_param'] ? '?'.$v['url_param'] : '';
            $v['url'] = '/'.$v['app'].'/'.$v['model'].'/'.$v['action'].$url;
            $v['icon'] = $v['icon'] ? $v['icon'] : 'layui-icon-triangle-r';
            $v['level'] = $tree->get_level($v['id'], $menu);
        }

        $tree->init($menu);
        foreach ($menu as $key => $value) {
            if ($value['level'] == 1) {
                $value['children'] = $tree->get_child($value['id']);
                $new_menu[$count++] = $value;
            }
        }

        $this->result($new_menu, 200);
    }

    // 获取用户名
    public function get_username()
    {
        Auth::is_login();
        $this->success($this->username);
    }
}
